package com.ddone.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ddone
 * @date 2024/3/22-12:12
 */
@Controller
@Slf4j
public class ApiController {

    @Autowired(required = false)
    private ServletContext servletContext;

    /**
     *
     *  TODO:
     *     我想要获取原生api对象！
     *     HttpServletRequest HttpServletResponse HttpSession  ServletContext
     *     注意： 前三个可以直接在形参列表声明 没有顺序要求 没有命名要求
     *            ServletContext 不能使用形参列表接值！
     *               1.  request 或者 session对象 获取getServletContext();
     *               2.  servletContext 全局唯一！ 这个会被DispatcherServlet放入ioc容器！ 自动注入
     * @param request
     * @param response
     * @param session
     * @return
     */
    @GetMapping("/api")
    public String api(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        log.warn("request= "+request+" response= "+response);
        ServletContext servletContext1 = request.getServletContext();
        log.warn("servletContext1 = " + servletContext1);
        log.warn("servletContext = " + servletContext);
        log.warn("session = " + session);
        return "hello";
    }

    @GetMapping("test")
    @ResponseBody
    public String test(){
        log.warn("测试中文字符");
        return "测试中文字符";
    }

}
